import http from './http'
import type {
  LoginResType,
  UserInfoDetailResType,
  UserListResType,
  AvatarFileResType
} from '@/interface/user/userType'
import { createBufferHashName } from '@/utils/utils'

/**
 *
 * @param data 包含用户名和密码登录信息
 * @returns
 */
export const loginAPI = (data: { username: string; md5pasw: string }): Promise<LoginResType> => {
  return http({
    url: '/user/login',
    method: 'POST',
    data
  })
}

// 获取用户信息
export const getUserInfoAPI = (): Promise<UserInfoDetailResType> => {
  return http({
    method: 'GET',
    url: `/user/userinfo`
  })
}

// 获取用户权限生菜菜单列表
export const getUserMenuAPI = (_id: string, role: number) => {
  return http({
    url: '/user/menu',
    method: 'GET',
    params: {
      _id,
      role
    }
  })
}

// 获取用户列表
export const getUserListAPI = (data: any): Promise<UserListResType> => {
  return http({
    method: 'GET',
    url: '/user/userlist',
    params: data
  })
}

// 删除某个用户列表用户
export const deleteUserListItemAPI = (id: number) => {
  return http({
    method: 'DELETE',
    url: `/user/userlist/${id}`
  })
}

// 添加用户
export const addUserItemAPI = (data: any) => {
  return http({
    method: 'POST',
    url: `/user/userlist`,
    data
  })
}

// 更新用户数据
export const updateUserInfoAPI = (id: string, data: any) => {
  return http({
    method: 'PATCH',
    url: `/user/userlist/${id}`,
    data
  })
}

// 上传用户头像
export const uploadUserAvatarAPI = async (
  file: any,
  filename: string
): Promise<AvatarFileResType> => {
  let fm = new FormData()
  fm.append('file', file)
  fm.append('filename', filename)
  return http({
    method: 'POST',
    url: `/user/uploadfile`,
    data: fm,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export const deleteAvatarFileAPI = (data: any) => {
  return http({
    method: 'DELETE',
    url: `/user/uploadfile`,
    data
  })
}
